package com.lagou.edu.boss.rest.authroity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.edu.authority.RoleRemoteService;
import com.lagou.edu.authority.dto.RoleDTO;
import com.lagou.edu.authority.param.RoleQueryParam;
import com.lagou.edu.boss.entity.form.RoleForm;
import com.lagou.edu.common.entity.vo.Result;
import com.lagou.edu.common.util.ConvertUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

/**
 * TODO
 *
 * @author lm
 * @since 2021/11/4 14:50
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleRemoteService roleRemoteService;

    @ApiOperation(value = "保存或更新角色")
    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdateRole(@RequestBody RoleForm roleForm) {
        RoleDTO roleDTO = ConvertUtils.convert(roleForm, RoleDTO.class);
        if (Objects.isNull(roleDTO.getId())) {
            //roleDTO.setCreatedBy(UserManager.getUserName());
            roleDTO.setCreatedBy("张三");
            roleDTO.setCreatedTime(new Date());
        }
        //roleDTO.setUpdatedBy(UserManager.getUserName());
        roleDTO.setUpdatedBy("张三");
        return roleRemoteService.saveOrUpdateRole(roleDTO);
    }

    @ApiOperation(value = "按条件查询角色")
    @PostMapping(value = "/getRolePages")
    public Result<Page<RoleDTO>> getRolePages(@RequestBody RoleQueryParam roleQueryParam) {
        return Result.success(roleRemoteService.getRolePages(roleQueryParam));
    }


}
